<?php
namespace Avalanche\Model;

use Avalanche\Model;

/**
 * Client Model is base model under each connection
 *
 * @author Vitold Sedyshev
 */
class ClientModel extends Model {

    /**
     * @var resource
     */
    protected $sock;

    /**
     * @var string
     */
    protected $requestBuffer = '';

    /**
     * @var string
     */
    protected $responseBuffer = '';

    /**
     * Set socket
     *
     * @param resource $sock
     * @return void
     */
    public function setSock($sock) {
        $this->sock = $sock;
    }

    /**
     * @param string $msg
     */
    public function send($msg)
    {
        socket_write($this->sock, $msg);
    }

    /**
     * Close connection with this client
     *
     * @return void
     */
    public function close() {
        socket_close($this->sock);
    }

    /**
     * @return resource
     */
    public function getSock()
    {
        return $this->sock;
    }

    /**
     * @param null|int $size
     */
    public function readRequestBuffer($size = null) {
        // To do ...
    }

    /**
     * @param string $requestBuffer
     */
    public function writeRequestBuffer($requestBuffer) {
        $this->requestBuffer .= $requestBuffer;
    }

    /**
     * @return string
     */
    public function getRequestBuffer()
    {
        return $this->requestBuffer;
    }

    /**
     * @param string $responseBuffer
     */
    public function writeResponseBuffer($responseBuffer)
    {
        $this->responseBuffer .= $responseBuffer;

        // All write to socket
        $this->send($this->responseBuffer);
    }

}
